#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <math.h>
#include <fstream>
#include <boost/filesystem/path.hpp>
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

#define DEBUG_LEVEL 6
#include <debug_func.h>


// my functions
#include <matrix_formats.h>
#include <print_error.h>
#include <program_options_helper.h>

using std::ofstream;
using std::ifstream;
using std::cout;
using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________


bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{
	string dbname;
	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	("dbname,d", po::value<string>(&dbname)->set_name("DATABASE"),
									"Existing PostgreSQL database name.")
	("species,s", po::value<string>(&args.species)->set_name("NAMESPACE"),
									"Panda namespace for species.\01"
									"E.g. 'homo', 'mus' or 'rattus'.");

	string host,user,password,input_file_name,extra_login;
	args.optional_args.add_options()
	("window_of_neighbour_genes,w", po::value<unsigned>(&args.window_size)
												->set_name("NUMBER")
												->default_value(10),
									"Distance in gene_positions up and down stream to search "
									"for homologs (i.e. align NUMBER neighbours upstream and "
									"NUMBER downstream).")
	("chromosome,c", po::value<string>(&args.chromosome)
												->set_name("NAME")
												->default_value("all"),
									"Calculate alignments for the specified chromosome.")
	("output_table,t", po::value<string>(&args.output_table)
												->set_name("TABLE")
												->default_value("TAXON.paralogs_align"),
									"Panda table for holding the alignment results")

	//____________________________________________________________________________________
	// panda login parameters
	("host,H", po::value<string>(&host)			->set_name("HOST"),
									"PostgreSQL database host computer address.")
	("user,u", po::value<string>(&user)			->set_name("USERNAME")
									   			->default_value("postgres"),
									"PostgreSQL database user name.")
	("password,p", po::value<string>(&password)	->set_name("PASSWORD"),
									"PostgreSQL database password.")
	("login,L", po::value<string>(&extra_login)	->set_name("OPTIONS"),
									"Other PostgreSQL login parameters in the form of "
									"keyword = value.\01E.g \"port=1234\"");
	//____________________________________________________________________________________





	args.add_std_options();

	po::options_description all_args("");
	all_args.add(mandatory_args).add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);


	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.0");
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
							"Calculates local alignments within +/- a specified "
							"number of gene positions.\n\n"
							"The following PostgreSQL tables are "
							"required:\01"
							"(TAXON is the namespace specified in --species)\01"
							"(OUTPUT is the table specified in --output_table)\n\n"
							"TABLE: TAXON.gene_loci "
							"(chromosome, gene_position, gene_id, ...)"				"\n"
							"TABLE: ens_id "
							"(gene_id, acc_code, pi, ...)"							"\n"
							"TABLE: sequences "
							"(pi, sequence, ...)"									"\n"
							"TABLE: OUTPUT "
							"(gene_id1, gene_id2, acc_code1, acc_code2, "
							"pi1, pi2, gene_position, gene_loci_diff, "
							"align_start, align_len, aligned_positions, coverage, "
							"e_value, len1, len2, aligned_residues, bitscore, "
							"gaps1, gaps2, ...)\n\n";

		print_usage(cerr, string(*argv), "--dbname DATABASE\n"
										"--species NAMESPACE\n"
										"--output_table TABLE\n"
										"[OPTIONS]",
							all_args, exe_description, 80);
		return false;
	}

	check_required_options(vm, mandatory_args);

	if (args.output_table == "TAXON.paralogs_align")
		args.output_table = args.species + ".paralogs_align";
	//
	// initialize postgresql login
	//
	dbname	= "dbname=" + dbname;
	if (host.length())
		host = "host=" + host;
	if (password.length())
		password = "password=" + password;
	user = "user=" + user;
	args.db_login = dbname + ' ' + host + ' ' + password + ' ' + user + ' ' + extra_login;





	args.open_err_log();
	return true;
}


